/**
 * 请在白鹭引擎的Main.ts中调用 platform.login() 方法调用至此处。
 */

class WxgamePlatform {

  name = 'wxgame'

  login() {
    return new Promise((resolve, reject) => {
      wx.login({
        success: (res) => {
          resolve(res)
        }
      })
    })
  }

  getUserInfo() {
    return new Promise((resolve, reject) => {
      wx.getUserInfo({
        withCredentials: true,
        success: function (res) {
          var userInfo = res.userInfo
          var nickName = userInfo.nickName
          var avatarUrl = userInfo.avatarUrl
          var gender = userInfo.gender //性别 0：未知、1：男、2：女
          var province = userInfo.province
          var city = userInfo.city
          var country = userInfo.country
          resolve(userInfo);
        }
      })
    })
  }
  previewImage(data) {
    wx.previewImage(data);
  }
  showModal(data) {
    wx.showModal(data);
  }
  request(url, data, callback, method, header) {
    return new Promise(() => {
      wx.request({
        url: url,
        data: data,
        method: method,
        header: header,
        success: function (res) {
          if (callback) {
            callback(res);
          }
        }, fail: function (err) {
          console.log("err", err);
        }, complete: function (res) {

        }
      })
    })
  }
  shareAppMessage(data) {
    wx.shareAppMessage(data);
  }
  getSettingInfo() {
    wx.getSetting({
      success: function (res) {
        var authSetting = res.authSetting
        if (authSetting['scope.writePhotosAlbum'] === true) {
          // 用户已授权，可以直接调用相关 API
          window["GameConfig"].storageType = 1;
          window["EventManager"].dispatchEvent(window["Notify"].CHECK_SCOPE);
        } else if (authSetting['scope.writePhotosAlbum'] === false) {
          // 用户已拒绝授权，再调用相关 API 或者 wx.authorize 会失败，需要引导用户到设置页面打开授权开关
          window["GameConfig"].storageType = 2;
          window["EventManager"].dispatchEvent(window["Notify"].CHECK_SCOPE);
        } else {
          // 未询问过用户授权，调用相关 API 或者 wx.authorize 会弹窗询问用户
          wx.authorize({
            scope: 'scope.writePhotosAlbum',
            fail: function (res) {
              // iOS 和 Android 对于拒绝授权的回调 errMsg 没有统一，需要做一下兼容处理
              if (res.errMsg.indexOf('auth deny') > -1 || res.errMsg.indexOf('auth denied') > -1) {
                // 处理用户拒绝授权的情况
                window["GameConfig"].storageType = 3;
                window["EventManager"].dispatchEvent(window["Notify"].CHECK_SCOPE);
              }
            },
            success: function (res) {
              window["GameConfig"].storageType = 1;
              window["EventManager"].dispatchEvent(window["Notify"].CHECK_SCOPE);
            }
          });
        }
      }
    })
  }
  saveImageToPhotosAlbum(path, success, fail) {
    wx.saveImageToPhotosAlbum({
      filePath: path,
      success: function (res) {
        if (success) {
          success();
        }
      },
      fail: function (res) {
        if (fail) {
          fail();
        }
      }
    });
  }
  openSetting() {
    wx.openSetting({
      success: function (res) {
        console.log("success");
      }
      // ,
      // fail: function (res) {
      //   console.log("fail");
      // }
      ,
      complete: function (res) {
        console.log("complete");
      }
    })
  }
  createOpenSettingButton(data) {
    let button = wx.createOpenSettingButton(data);
    return button;
  }
  createGameClubButton(data) {
    var lf = data.left * window["GameConfig"].scale;
    data = {
      icon: "green",
      style: {
        left: data.style.left * window["GameConfig"].scale,
        top: data.style.top * window["GameConfig"].scale,
        width: data.style.width * window["GameConfig"].scale,
        height: data.style.height * window["GameConfig"].scale
      }
    }
    var btn = wx.createGameClubButton(data);
    return btn;
  }
  getSystemInfo(data) {
    wx.getSystemInfo(data);
  }
  getSystemInfoSync(callBack) {
    wx.getSystemInfo({
      success: function (res) {
        callBack(res);
      }
    });
  }
  getLaunchOptionsSync(callback) {
    var info = wx.getLaunchOptionsSync();
    console.log("getLaunchOptionsSync", info);
    var ticket = "";
    if (info.scene == 1044) {
      ticket = info.shareTicket;
    }
    callback(ticket, info);
  }

  openCustomerServiceConversation(data) {
    wx.openCustomerServiceConversation(data);
  }
  setUserCloudStorage(data, callback) {
    return new Promise(() => {
      wx.setUserCloudStorage({
        KVDataList: data,
        success: function (res) {
          if (callback) {
            callback(res)
          }
        },
        fail: function (res) {
          console.log(res)
        }
      })
    })
  }
  /**调用微信的方法，name:方法名，data:参数 */
  callPlatformMethod(name, data) {
    if (name) {
      var method = wx[name];
      if (method && (typeof method == "function")) {
        method(data);
      }else{
        if (data && data.fail && (typeof data.fail == "function")){
          data.fail(name+":fail");
        }
        if (data && data.complete && (typeof data.complete == "function")) {
          data.complete(name + ":fail");
        }
      }
    }
  }
  createWorker(path) {
    let worker = wx.createWorker(path);
    return worker;
  }
}


window.platform = new WxgamePlatform();
